package logic

import (
	"community/communityrpc/communityrpc"
	"community/communityrpc/internal/svc"
	"community/models/mysqlinit"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectLogic) Perfect(in *communityrpc.PerfectRequest) (*communityrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var user mysqlinit.User

	mysqlinit.DB.Debug().Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("没有此用户，请重新输入")
	}

	m := mysqlinit.User{

		Portrait:   in.Portrait,
		Background: in.Name,
		Name:       in.Name,
		Gender:     in.Gender,
		Personal:   in.Personal,
		Birthday:   in.Birthday,
		Term:       in.Term,
		Address:    in.Address,
	}
	mysqlinit.DB.Debug().Where("id=?", in.UserId).Updates(&m)

	return &communityrpc.PerfectResponse{
		Id: int64(user.ID),
	}, nil
}
